package com.ccg.plat.presentation.ui.feature5.viewmodel

import android.app.Activity
import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.ccg.plat.data.local.cache.UserInfoCache
import com.ccg.plat.presentation.common.base.viewmodel.BaseViewModelC
import com.ccg.plat.presentation.common.util.NavigationUtils
import com.ccg.plat.presentation.ui.feature4.model.VideoBean
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlin.random.Random

/**
 * @author : C4_雍和
 * 描述 :
 * 主要功能 :
 * 维护人员 : C4_雍和
 * date : 2025/8/9 8:53
 */
class VideoListViewModel(application: Application) : BaseViewModelC(application) {
    //定义意图
    sealed class ContentIntent {
        class GoVideoPlayerActivity(val activity: Activity, val data: VideoBean) : ContentIntent()
        class GoRandomVideoPlayer(val activity: Activity) : ContentIntent()
        class RemoveVideoForIndex(val index: Int) : ContentIntent()
    }

    data class NaviViewState(var uiListData: List<VideoBean> = emptyList(), var tips: String = "")

    val newsChannel = Channel<ContentIntent>(Channel.UNLIMITED)
    var uiState by mutableStateOf(NaviViewState())
    private var saveKey = ""

    init {
        handleIntent()
    }

    private fun handleIntent() {
        launch(Dispatchers.IO) {
            newsChannel.consumeAsFlow().collect { data ->
                when (data) {
                    is ContentIntent.GoVideoPlayerActivity -> {
                        if (UserInfoCache.isDefault()) {
                            NavigationUtils.goVideoPlayerActivity(context = data.activity, json = GsonBuilder().create().toJson(data.data))
                        } else {
                            NavigationUtils.goVideoPlayerTwoActivity(context = data.activity, json = GsonBuilder().create().toJson(data.data))
                        }
                    }

                    is ContentIntent.GoRandomVideoPlayer -> {
                        if (UserInfoCache.isDefault()) {
                            NavigationUtils.goVideoPlayerActivity(context = data.activity, json = GsonBuilder().create().toJson(uiState.uiListData[Random.nextInt(0, uiState.uiListData.size)]))
                        } else {
                            NavigationUtils.goVideoPlayerTwoActivity(context = data.activity, json = GsonBuilder().create().toJson(uiState.uiListData[Random.nextInt(0, uiState.uiListData.size)]))
                        }
                    }

                    is ContentIntent.RemoveVideoForIndex -> {
                        val mList = uiState.uiListData.toMutableList()
                        mList.removeAt(data.index)
                        MMKV.defaultMMKV().encode(saveKey, GsonBuilder().create().toJson(mList))
                        uiState = uiState.copy(uiListData = mList)
                    }
                }
            }
        }
    }

    suspend fun initData(url: String?) {
        if (url.isNullOrEmpty()) {
            uiState = uiState.copy(tips = "服务器有问题请联系管理员")
        } else {
            saveKey = url
            val mJson = MMKV.defaultMMKV().decodeString(url, "")
            if (mJson.isNullOrEmpty()) {
            } else {
                val listData = GsonBuilder().create().fromJson<List<VideoBean>>(mJson, object : TypeToken<List<VideoBean>>() {}.type)
                uiState = uiState.copy(uiListData = listData)
            }
        }
    }
}